<template>
  <div id="app">
    <van-form @submit="onSubmit" class="container">
      <h2>欢迎来到易购</h2>
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="若未注册，直接自定义账户即可"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        class="psd"
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { Toast } from "vant";
import axios from "axios";
import { mapMutations, mapActions } from "vuex";
export default {
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    async onSubmit() {
      let res = await axios({
        method: "post",
        url: "http://kumanxuan1.f3322.net:8001/auth/loginByWeb",
        data: {
          username: this.username,
          pwd: this.password,
        },
      });
      let data = res.data;
      console.log(data);
      if (data.errno == 0) {
        let token = data.data.token;
        let user = {
          id: data.data.userInfo.id,
          username: data.data.userInfo.username,
          nickname: data.data.userInfo.nickname,
          url: "https://ts1.cn.mm.bing.net/th/id/R-C.20c006e2be3e037453c4c61d0cd4d158?rik=OkDPMr6V%2bOtqzA&riu=http%3a%2f%2fimg.crcz.com%2fallimg%2f201911%2f21%2f1574317250119426.jpg&ehk=1fwCPeZoFoZ9gfoFUIq5oVjp33WiwSGdUrqIf%2fnkbww%3d&risl=&pid=ImgRaw&r=0&sres=1&sresct=1",
        };
        this.$store.dispatch("updateUser", user);
        this.$store.dispatch("updateToken", token);
        Toast.success("登录成功！");
        this.$router.push({ path: "/mine" });
      } else {
        let msg = data.errmsg;
        Toast.fail(msg);
      }
    },
  },
};
</script>

<style scoped>
#app {
  border-top: 0.01rem solid transparent;
  width: 100vw;
  height: 100vh;
  background-image: url("https://pic4.zhimg.com/v2-d877a40b104fb966a750809d57788d73_r.jpg?source=1940ef5c");
}
#app .container {
  width: 80vw;
  height: 50vh;
  margin: 20vh auto;
}
#app .container .psd {
  margin-top: 0.3rem;
}
</style>